package com.xgkj.diyiketang.http.engine;


import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.BaseActivity;
import com.xgkj.diyiketang.activity.LoginActivity;


public class ShowToastTaskEventListener implements TaskEventListener{
	private Context mContext;
	public ShowToastTaskEventListener(Context context){
		mContext = context;
	}

	@Override
	public void handleTaskCompleted(int taskId, int result, Object data) {
		
	}

	@Override
	public void handleTaskError(int taskId, int errorCode, String errorMsg) {
		if(mContext == null)return;
		switch (errorCode) {
		case TaskResultCode.NETWORK_CONNECT_ERROR:
			Toast.makeText(mContext, R.string.no_network, Toast.LENGTH_SHORT).show();
			break;
		case TaskResultCode.SERVER_CONNECT_ERROR:
			Toast.makeText(mContext, R.string.no_server, Toast.LENGTH_SHORT).show();
			break;
		case TaskResultCode.NEED_LOGIN:
			Intent  intent  = new Intent(mContext, LoginActivity.class);
			if (mContext instanceof BaseActivity) {
				mContext.startActivity(intent);
			}else {
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				mContext.startActivity(intent);
			}
			Toast.makeText(mContext, R.string.needlogin, Toast.LENGTH_SHORT).show();
			break;

		default:
			if(errorMsg != null){
				Toast.makeText(mContext, errorMsg, Toast.LENGTH_SHORT).show();
			}
			break;
		}
	}

	@Override
	public void handleTaskMessage(int taskId, Object data) {
		
	}

}
